package org.bangumibuddy.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.bangumibuddy.constant.DownloadStatus;
import org.bangumibuddy.entity.TorrentEntity;
import org.bangumibuddy.mapper.RssMapper;
import org.bangumibuddy.mapper.TorrentsMapper;
import org.bangumibuddy.pojo.DownloadInfo;
import org.bangumibuddy.service.TorrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TorrentServiceImpl implements TorrentService {

    @Autowired
    TorrentsMapper torrentsMapper;

    @Override
    public List<DownloadInfo> getDownloadInfoByAnimeId(Long AnimeId) {
        List<TorrentEntity> torrentsByAnimeId = torrentsMapper.getTorrentsByAnimeId(AnimeId);
        return CreateDownloadInfos(torrentsByAnimeId);
    }

    @Override
    public List<DownloadInfo> getDownloadInfoByRssId(Long rssId) {
        List<TorrentEntity> torrentsByRssId = torrentsMapper.getTorrentsByRssId(rssId);
        return CreateDownloadInfos(torrentsByRssId);
    }

    private List<DownloadInfo> CreateDownloadInfos(List<TorrentEntity> torrentEntities)
    {
        List<DownloadInfo> downloadInfo = new ArrayList<>();
        for (TorrentEntity torrent : torrentEntities) {
            DownloadInfo info = new DownloadInfo();
            info.setUrl(torrent.getUrl());
            info.setTorrentName(torrent.getName());
            info.setTotalSize(torrent.getSize());
            info.setHash(torrent.getHash());
            info.setStatus(DownloadStatus.MISSING_FILE);
            downloadInfo.add(info);
        }

        return downloadInfo;
    }
}
